/* USER CODE BEGIN 1 */
/**
  * pm2.5, pm10 传感器
  * uart1, 115200
  * 启动测量	0x7E 0x00 0x00 0x02 0x01 0x03 0xF9 0x7E
  * 读取ID	0x7E 0x00 0xD0 0x01 0x00 0x2E 0x7E
  * 读取数据	0x7E 0x00 0x03 0x00 0xFC 0x7E
  * 关闭测量	0x7E 0x00 0x01 0x00 0xFE 0x7E
  * 睡眠	0x7E 0x00 0x10 0x00 0xEF 0x7E
  * 唤醒	0x7E 0x00  0x11 0x00 0xEE 0x7E
  * 复位	0x7E 0x00 0xD3 0x00 0x2C 0x7E
  *
  *
  * NO2/O3
  * uart1, 9600
  * 使用默认的查询工作模式
  * 获取传感器类型，最大量程，单位，单位小数位数指令：0xD1
  * 获取传感器类型，最大量程，单位，单位小数位数指令：0xD7
  * 主动读气体浓度值 0xD7
  * 获取当前温度和湿度 0xD2
  * 获取当前温度和湿度带校验 0xD6
  * 获取当前版本号 0xD3
  *
  *
  *
  */
#include <stdio.h>
#include <string.h>
#include "sensor.h"
#include "usart.h"

//------------------------------------------------------------------------------------------
typedef struct tagSensorState
{
	uint8_t sts; 		//
	uint8_t wait_rsp;	//是否在等待回应中
}SensorState;

int sensor_cnt;
SensorState sensor_state;
Uart_Sensors   Sensors_Data;
#if USE_CMD_BUF
uint16_t cmd_len=0;
uint8_t  cmd_buf[100]={0};
#endif

uint8_t TB600B_readtype_decimal[1] = {0xD1};
uint8_t TB600B_auto[9] = {0XFF,0X01,0X78,0X40,0X00,0X00,0X00,0X00,0X47};

uint8_t SPS30_start[8] = {0x7E,0x00,0x00,0x02,0x01,0x03,0xF9,0x7E};
uint8_t SPS30_readid[7] = {0x7E,0x00,0xD0,0x01,0x00,0x2E,0x7E};
uint8_t SPS30_readdata[6] = {0x7E,0x00,0x03,0x00,0xFC,0x7E};
uint8_t SPS30_ID[16] = {0x7E, 0x00, 0xD0, 0x00, 0x09, 0x30, 0x30, 0x30, 0x38, 0x30, 0x30, 0x30, 0x30, 0x00, 0x9B, 0x7E};

//------------------------------------------------------------------------------------------
void sensor_PM_Sps30_loop(void);
void sensor_O3_loop(void);
void sensor_NO2_loop(void);
//------------------------------------------------------------------------------------------
/**
 * 初始化传感器相关
 */
void sensor_init(uint8_t type)
{
	sensor_cnt = 0;
	memset(&sensor_state, 0, sizeof(SensorState));
#if USE_CMD_BUF
	cmd_len = 0;
#endif

	switch(type){
	case SENSOR_PM_Sps30:
		UART1_SetBaudRate(115200U);
		break;
	case SENSOR_O3:
		UART1_SetBaudRate(9600U);
		break;
	case SENSOR_NO2:
		UART1_SetBaudRate(9600U);
		break;
	default:
		break;
	}
}
/**
* 在主循环中连续处理
*/
void sensor_loop(uint8_t type)
{
	switch(type){
	case SENSOR_PM_Sps30:
		sensor_PM_Sps30_loop();
		break;

	case SENSOR_O3:
		sensor_O3_loop();
		break;

	case SENSOR_NO2:
		sensor_NO2_loop();
		break;

	default:
		break;
	}
}
void sensor_PM_Sps30_loop(void)
{
	//判断是否有数据返回
	uint16_t len;
	uint8_t* dat;

#if USE_CMD_BUF
	len = cmd_len;
	dat = cmd_buf;

	cmd_len = 0;
#else
	len = usart_buf[UART_ID_SENSOR].rx_len;
	dat = (uint8_t *)usart_buf[UART_ID_SENSOR].rx_buf;

	usart_buf[UART_ID_SENSOR].rx_len = 0;

#endif

	if(len > 0){
		sensor_state.wait_rsp = 0; //数据已经返回，取消等待标记
		//printf("sensor data length: %d, dat[0]=%02x\r\n", len, dat[0]);
//		UART2_SendBytes(dat, len);

		if(0 == sensor_state.sts){ //当前是读类型的状态
			sensor_state.sts = 1;
			printf("satared\n");
		}//可以做其它操作
		else if(1 == sensor_state.sts){
			printf("readed\n");

			if(len == 7)
			{

				printf("no data\n");
			}
			else
			{
				Sensors_Data.PM25µg =  ((uint32_t)dat[4]<<24) +((uint32_t)dat[5]<<16)+ ((uint32_t)dat[6]<<8)+ ((uint32_t)dat[7]);
				Sensors_Data.PM10µg =  ((uint32_t)dat[12]<<24) +((uint32_t)dat[13]<<16)+ ((uint32_t)dat<<8)+ ((uint32_t)dat[15]);
				Sensors_Data.PM25cm =  ((uint32_t)dat[24]<<24) +((uint32_t)dat[25]<<16)+ ((uint32_t)dat[26]<<8)+ ((uint32_t)dat[27]);
				Sensors_Data.PM10cm =  ((uint32_t)dat[32]<<24) +((uint32_t)dat[33]<<16)+ ((uint32_t)dat[34]<<8)+ ((uint32_t)dat[35]);

				char message[250];
				sprintf(message, "PM25µg: %u, PM10µg: %u, PM25cm: %u, PM10cm: %u\r\n", Sensors_Data.PM25µg, Sensors_Data.PM10µg,Sensors_Data.PM25cm,Sensors_Data.PM10cm);
				HAL_UART_Transmit(&huart2, (uint8_t*) message, strlen(message), 20);
			}
		}
		sensor_cnt = 0;
	}
	else if(0==sensor_cnt){
		printf("ok\n");
		if(0 == sensor_state.sts) { //第一步读类型

			UART1_SendBytes(SPS30_start, sizeof(SPS30_start)); //读类型
			sensor_cnt = 5000;
			sensor_state.wait_rsp = 1; //发送了命令，设置等待标记
		}
		else if(1 == sensor_state.sts) {
			UART1_SendBytes(SPS30_readdata, sizeof(SPS30_readdata));//读类型
			sensor_state.wait_rsp = 1; //发送了命令，设置等待标记
			sensor_cnt = 5000;
		}
	}else{
		sensor_cnt--;
	}
}
/***
 * 处理 O3 传感器
 */
void sensor_O3_loop(void)
{
	//判断是否有数据返回
	uint16_t len;
	uint8_t* dat;

#if USE_CMD_BUF
	len = cmd_len;
	dat = cmd_buf;

	cmd_len = 0;
#else
	len = usart_buf[UART_ID_SENSOR].rx_len;
	dat = (uint8_t *)usart_buf[UART_ID_SENSOR].rx_buf;

	usart_buf[UART_ID_SENSOR].rx_len = 0;

#endif

	if(len > 0){
		sensor_state.wait_rsp = 0; //数据已经返回，取消等待标记
		//printf("sensor data length: %d, dat[0]=%02x\r\n", len, dat[0]);
//		UART2_SendBytes(dat, len);

		if(0 == sensor_state.sts){ //当前是读类型的状态
			sensor_state.sts = 1;
			uint8_t decimal = (dat[7] & 0xF0) >> 4 ;	 //dat7位的bit[4]~bit[7]|小数位数最多3位 | bit[0]~bit[3])数据正负号0(正数) 1(负数)
			Sensors_Data.Decimal = pow(10, decimal);
		}//可以做其它操作
		else if(1 == sensor_state.sts){

			uint16_t val;
			val = dat[2];
			val <<= 8;
			val += dat[3];
			Sensors_Data.O3_data_ug =  val / Sensors_Data.Decimal;
			val = dat[6];
			val <<= 8;
			val += dat[7];
			Sensors_Data.O3_data_ppb =  val / Sensors_Data.Decimal;


			char message[50];
			sprintf(message, "O3_ug: %u ug, O3_ppb: %u ppb\r\n", Sensors_Data.O3_data_ug, Sensors_Data.O3_data_ppb);
			HAL_UART_Transmit(&huart2, (uint8_t*) message, strlen(message), 20);


		}

	}
	else{
		if(0 == sensor_state.sts) { //第一步读类型

			UART1_SendBytes(TB600B_readtype_decimal, 1); //读类型
			sensor_state.wait_rsp = 1; //发送了命令，设置等待标记

		}
		else if(1 == sensor_state.sts) {
			UART1_SendBytes(TB600B_auto, sizeof(TB600B_auto)); //读类型
			sensor_state.wait_rsp = 1; //发送了命令，设置等待标记
		}
	}
}
/***
 * 处理 NO2 传感器
 */
void sensor_NO2_loop(void)
{
	//判断是否有数据返回
	uint16_t len;
	uint8_t* dat;

#if USE_CMD_BUF
	len = cmd_len;
	dat = cmd_buf;

	cmd_len = 0;
#else
	len = usart_buf[UART_ID_SENSOR].rx_len;
	dat = (uint8_t *)usart_buf[UART_ID_SENSOR].rx_buf;

	usart_buf[UART_ID_SENSOR].rx_len = 0;

#endif

	if(len > 0){
		sensor_state.wait_rsp = 0; //数据已经返回，取消等待标记
		//printf("sensor data length: %d, dat[0]=%02x\r\n", len, dat[0]);
//		UART2_SendBytes(dat, len);

		if(0 == sensor_state.sts){ //当前是读类型的状态
			sensor_state.sts = 1;
			uint8_t decimal = (dat[7] & 0xF0) >> 4 ;	 //dat7位的bit[4]~bit[7]|小数位数最多3位 | bit[0]~bit[3])数据正负号0(正数) 1(负数)
			Sensors_Data.Decimal = pow(10, decimal);
		}//可以做其它操作
		else if(1 == sensor_state.sts){

			Sensors_Data.NO2_data_mg =  (((uint16_t)dat[2]<< 8) + (uint16_t)dat[3]) / Sensors_Data.Decimal;
			Sensors_Data.NO2_data_ppm =  (((uint16_t)dat[6]<< 8) + (uint16_t)dat[7]) / Sensors_Data.Decimal;

			char message[50];
			sprintf(message, "NO2_mg: %u mg, NO2_ppm: %u ppm\r\n", Sensors_Data.NO2_data_mg, Sensors_Data.NO2_data_ppm);
			HAL_UART_Transmit(&huart2, (uint8_t*) message, strlen(message), 20);


		}

	}
	else{
		if(0 == sensor_state.sts) { //第一步读类型

			UART1_SendBytes(TB600B_readtype_decimal, 1); //读类型
			sensor_state.wait_rsp = 1; //发送了命令，设置等待标记

		}
		else if(1 == sensor_state.sts) {
			UART1_SendBytes(TB600B_auto, sizeof(TB600B_auto)); //读类型
			sensor_state.wait_rsp = 1; //发送了命令，设置等待标记
		}
	}
}
/**
 * 串口接收到数据之后，把数据保存到这个缓冲区
 */
void sensor_set_data(const uint8_t* data, int length)
{
#if USE_CMD_BUF
	int len;

	len = length;
	if(len > (int)sizeof(cmd_buf)){
		len = (int)sizeof(cmd_buf);
	}
	memcpy(cmd_buf, data, len);
	cmd_len = len;
#endif
}
/* USER CODE END 1 */
